<template>
    <div class="edit-popup" name="appleave">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="姓名">
                    <el-input v-model="formData.realname" disabled />
                </el-form-item>
                <el-form-item label="单位">
                    <el-input v-model="formData.deptname" disabled />
                </el-form-item>
                <el-form-item label="职务">
                    <el-input v-model="formData.PositionName" disabled />
                </el-form-item>
                <el-form-item label="工作时间">
                    <el-input v-model="formData.JoinWorkDateTime" disabled />
                </el-form-item>
                <el-form-item label="工作年限">
                    <el-input v-model="formData.WorkYears" disabled />
                </el-form-item>
                <el-form-item label="职务类别">
                    <el-input v-model="formData.PositionTypeName" disabled />
                </el-form-item>
                <el-form-item label="请假类别" prop="holiday">
                    <el-input v-model="formData.holiday_text" disabled />
                </el-form-item>
                <el-form-item label="请假事由" prop="memo">
                    <el-input v-model="formData.memo" disabled />
                </el-form-item>
                <el-form-item label="请假时间" prop="leaveTime">
                    <el-input v-model="formData.leaveTime" disabled />
                </el-form-item>
                <el-form-item label="是否离连">
                    <el-tag v-if="formData.isout==1" effect="dark" type="danger">离连</el-tag>
                    <el-tag v-if="formData.isout==0" effect="dark" type="primary">在连</el-tag>
                </el-form-item>
                <el-form-item label="请假附件" prop="filelist">
                    <div>
                        <el-text v-for="(itemlink, linkkey) in formData.filelist" :key="linkkey">
                            <el-link :href="itemlink.uri" target="_blank">{{ itemlink.name }}</el-link>
                            <!-- <el-button @click="onDelFile(linkkey)">删除</el-button> -->
                        </el-text>
                        <upload type="file" @success="onUploadSuccess" :show-progress="true">
                            <el-button type="primary">上传附件(选填)</el-button>
                        </upload>
                        <div class="form-tips">支持文件格式:图片.jpg .png 文档：word ppt excel pdf</div>
                    </div>
                </el-form-item>
                <el-form-item label="审核理由" prop="appmemo">
                    <el-input v-model="formData.appmemo" placeholder="审批理由(选填)" clearable />
                </el-form-item>
                <el-form-item label="审批状态" >
                    <div>
                    <el-radio-group v-model="formData.appResult">
                    <el-radio value="10" size="large">同意</el-radio>
                    <el-radio value="-10" size="large">驳回</el-radio>
                    <el-radio value="-20" size="large">终止</el-radio>
                    </el-radio-group>
                    <div class="form-tips">驳回:发起人可以修改后，重新提交审核;</div>
                    <div class="form-tips">终止:终止整个审批流程，发起人无法修改;</div>
                    </div>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="appleave">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import useUserStore from '@/stores/modules/user'
import { useDictData } from '@/hooks/useDictOptions'
import Upload from '@/components/upload/index.vue'
import { leaveAdd, leaveAppEdit, leaveAppDetail } from '@/api/leave'
const userStore = useUserStore()
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const { dictData: optionData } = useDictData('Position,PositionType,holiday')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '审批请假' : '新增请假'
})
/**从文件列表中删除文件 */
const onDelFile = (index: any) => {
    formData.filelist.splice(index, 1)
}
//const filelist = ref<any[]>([])
const onUploadSuccess = (file: any) => {
    formData.filelist.push(file.data);
    console.log('文件上传成功', file.data)
    // console.log('用户列表数据', pager.lists)
    // Object.keys(pager.lists).forEach((key) => {
    //     if (pager.lists[key]['id'] == file.data['mid']) {
    //         pager.lists[key]['avatar'] = file.data['uri']
    //     }
    // })
    //getLists()
}
const formData = reactive({
    id: '',
    realname: '',
    PositionName: '',
    holiday_text: '',
    WorkYears: 0,
    appmid: userStore.userInfo.id,
    appmemo: '',
    appResult: '10',
    PositionTypeName: '',
    JoinWorkDateTime: '',
    member_id: 0,
    deptname:'',
    holiday: '',
    memo: '',
    leaveTime: '',
    isout: 0,
    filelist: [] as any
})

const formRules = {
  
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    await leaveAppEdit(formData)
    console.log('提交信息', formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await leaveAppDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
